package com.example.test1.controller;

import com.example.test1.Dto.ResponseDTO;
import com.example.test1.Dto.WordDetailsDTO;
import com.example.test1.Dto.WordOpDTO;
import com.example.test1.Dto.WordProficiencyDTO;
import com.example.test1.service.WordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/word", produces = "application/json")
public class WordController {
    @Autowired
    private WordService wordService;

    /**
     * 搜索单词
     */

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public ResponseDTO<Object> search(@RequestBody WordOpDTO wordOpDto) {
        WordDetailsDTO wordDetailsDto = wordService.search(wordOpDto);
        return new ResponseDTO<>("200", "success", wordDetailsDto);
    }

    /**
     * 收藏单词
     */

    @RequestMapping(value = "/collect", method = RequestMethod.POST)
    public ResponseDTO<Object> collect(@RequestBody WordOpDTO wordOpDto) {
        wordService.collect(wordOpDto);
        return new ResponseDTO<>("200", "success", null);
    }

    /**
     * 取消单词的收藏
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseDTO<Object> delete(@RequestBody WordOpDTO wordOpDto) {
        wordService.delete(wordOpDto);
        return new ResponseDTO<>("200", "success", null);
    }

    /**
     * 随机获取几个单词
     */

    @RequestMapping(value = "/random", method = RequestMethod.POST)
    public ResponseDTO<Object> random(@RequestBody WordOpDTO wordOpDto) {
        List<WordDetailsDTO> randomWords = wordService.randomWord(wordOpDto.getOpenId());
        return new ResponseDTO<>("200", "success", randomWords);
    }

    /**
     * 查询收藏的所有单词
     */

    @RequestMapping(value = "/review", method = RequestMethod.POST)
    public ResponseDTO<Object> review(@RequestBody WordOpDTO wordOpDto) {
        Map<Long, WordDetailsDTO> words = wordService.getCollectedWords(wordOpDto.getOpenId());
        return new ResponseDTO<>("200", "success", words);
    }

    /**
     * 设置单词熟练度
     */

    @RequestMapping(value = "/setProficiency", method = RequestMethod.POST)
    public ResponseDTO<Object> setProficiency(@RequestBody WordProficiencyDTO wordProficiencyDto) {
        wordService.setWordProficiency(wordProficiencyDto);
        return new ResponseDTO<>("200", "success", null);
    }

    /**
     * 初始化，将单词存入redis数据库
     */

    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public ResponseDTO<Object> initWords() {
        wordService.initWords();
        return new ResponseDTO<>("200", "success", null);
    }

}
